package com.hyt.it.ogt.kq.service.gov.den;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.common.config.ConfigManager;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.gov.feign.tps.GovTpsClient;
import com.hyt.model.tps.vo.Bucket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author huangyh
 */
@Slf4j
@Component
public class TencentCloudCosUtil {

    /**
     * 注入配置类
     */
    @Resource
    private ConfigManager configManager;

    /**
     * 注入tpsClient
     */
    @Resource
    private GovTpsClient tpsClient;

    /**
     * 赋值静态变量
     */
    @PostConstruct
    public void init() {
        TencentCloudCosUtil.config = configManager;
        TencentCloudCosUtil.client = tpsClient;
    }

    /**
     * 静态配置类
     */
    private static ConfigManager config;

    /**
     * 静态tpsClient
     */
    private static GovTpsClient client;

    /**
     * 获取腾讯云COS考生照片存储路径
     * @param taskId 考试任务id
     * @return 腾讯云COS考生照片存储路径
     */
    public static String getPhotoRootPath(String taskId) {
        return "/" + config.getCloudRootPath() +
                taskId + "/" + config.getCandidatePhotoPath() + "/";
    }

    /**
     * 获取腾讯云COS文件全路径
     * @param officeId 机构id
     * @param path 文件半路径
     * @return 腾讯云COS文件全路径
     */
    public static String getAllPath(String officeId, String path) {
        ApiResponse<Bucket> response = client.getTencentCosTempCredential(officeId);
        if (response.getCode() != 1) {
            throw new KqException(ResponseCode.FAIL_TO_GET_BUCKET_INFO.getCode(), ResponseCode.FAIL_TO_GET_BUCKET_INFO.getMsg());
        }
        String domain = response.getData().getDomain();
        return domain + path;
    }

    /**
     * 获取腾讯云COS桶信息
     * @param officeId 机构id
     * @return 腾讯云COS桶信息
     */
    public static Bucket getBucket(String officeId) {
        ApiResponse<Bucket> response = client.getTencentCosTempCredential(officeId);
        if (response.getCode() != 1) {
            throw new KqException(ResponseCode.FAIL_TO_GET_BUCKET_INFO.getCode(),ResponseCode.FAIL_TO_GET_BUCKET_INFO.getMsg());
        }
        return response.getData();
    }

}
